<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;

/**
 * Description of IndexController
 *
 * @author billy
 */
class IndexController extends CommonController {
    //put your code here
    public function share(Request $request) {
        view()->share('user', $request->session()->get("Home"));
    }
    /**
     * 执行用户登录操作
     * @param Request $request
     */
    public function doLogin(Request $request) {
        $res = \App\Models\MlcmsUser::AuthLogin($request);
        if ($res['status'] == 0) {
            return view("dispatch", ["message"=>false,"error"=>$res['info'], "waitSecond"=>3,"jumpUrl"=>url("/")]);
        }
        $request->session()->put("Home", $res['info']);
        $request->session()->save();
        return view("dispatch",["message"=>"登录成功","error"=>false, "waitSecond"=>1,"jumpUrl"=>url("index/index")]);
    }
    /**
     * 系统首页
     * @param Request $request
     */
    public function index(Request $request) {
        //新闻公告
        $news = \App\Models\MlcmsNew::getNewsLimt(10);
        $assign = compact('news');
        $this->share($request);
        return view("Home.index", $assign);
    }
    /**
     * 新闻详情
     */
    public function newDetail(Request $request, $id) {
        $new = \App\Models\MlcmsNew::getNewById($id);
        if (!$new) {
            return view("dispatch", ["message"=>false,"error"=>"新闻不存在", "waitSecond"=>3,"jumpUrl"=>url("index/index")]);
        }
        $this->share($request);
        return view("home.newdetail", compact('new'));
    }
    /**
     * 新闻中心
     * @param Request $request
     * @param type $id
     */
    public function newCenter(Request $request) {
        $news = \App\Models\MlcmsNew::OrderBy("Mn_updateTime", "desc")->paginate();
        
        $this->share($request);
        return view("home.newcenter", compact('news'));
    }
    /**
     * 收件箱
     * @param Request $request
     */
    public function inBox(Request $request) {
        $user = $request->session()->get("Home");
        $messages = \App\Models\Messagelist::where("AcceptUserId",$user->Mu_Number)->OrWhere('AcceptUserId', "ALL")->paginate();
        
        $this->share($request);
        return view("home.inbox", compact('messages'));
    }
    /**
     * 发件箱
     * @param Request $request
     */
    public function sendBox(Request $request) {
        $user = $request->session()->get("Home");
        if ($request->input("_token")) {
           $res = \App\Models\Messagelist::addMessage($request, $user);
           if ($res['status'] == 0) {
               return view("dispatch", ["message"=>false,"error"=>$res['info'], "waitSecond"=>3,"jumpUrl"=>url("index/sendbox")]);
           }
           return view("dispatch", ["message"=>$res['info'],"error"=>false, "waitSecond"=>3,"jumpUrl"=>url("index/sendbox")]);
        }
        $messages = \App\Models\Messagelist::where("SendUserId", $user->Mu_Number)->paginate();
        $this->share($request);
        return view("home.sendbox",compact('messages'));
    }
    /**
     * 验证用户二级密码
     * @param Request $request
     */
    public function secLogin(Request $request) {
        if ($request->input("_token")) {
            $user = $request->session()->get("Home");
            if ($user->Mu_TwoPwd == aaa_md5($request->input('Mu_TwoPwd'))) {
                $user->authSecPwd = true;
            }
            $jumpurl = $request->input("_jumpurl", "index/index");
            return redirect($jumpurl);
        }
        $jumpurl = session('url') ? session('url') : $request->input('_jumpurl');
        $this->share($request);
        return view("home.seclogin",compact('jumpurl'));
    }
    //修改用户信息
    public function userInfo(Request $request) {
        if ($request->input("_token")) {
            $res = \App\Models\MlcmsUser::modifyUser($request);
            return aaa_dispatch($res['status'], $res['info'], url("index/userinfo"));
        }
        $banks = \App\Models\MlcmsBank::get();
        $this->share($request);
        return view("home.userinfo", compact('banks'));
    }
    /**
     * 修改密码
     * @param Request $request
     */
    public function modpwd(Request $request) {
        if ($request->input("_token")) {
            $res = \App\Models\MlcmsUser::modPwd($request);
            return aaa_dispatch($res['status'], $res['info'],url("index/modpwd"));
        }
        $this->share($request);
        return view("home.modpwd");
    }
    /**
     * 会员升级
     */
    public function upgrade(Request $request) {
        if ($request->input("_token")) {
            $res = \App\Models\MlcmsUser::upgradeUser($request);
            return aaa_dispatch($res['status'], $res['info'],url("index/upgrade"));
        }
        $this->share($request);
        return view("home.upgrade");
    }
    
    /**
     * 奖金明细
     */
    public function countMoneyLog(Request $request) {
        $user = $request->session()->get("Home");
        $mlogs = \App\Models\MlcmsCountmoneylog::where('Number', $user->Mu_Number)->orderBy('CreateTime', 'desc');
        if ($request->input('starttime')) {
            $mlogs->where('CreateTime','>=',$request->input('starttime'));
        }
        if ($request->input('endtime')) {
            $mlogs->where('CreateTime', '<=', $request->input('endtime'));
        }
        $mlogs = $mlogs->paginate();
        $this->share($request);
        return view("home.countmoneylog", compact('mlogs','request'));
    }
    
    /**
     * 奖金提现
     */
    public function withdraw(Request $request) {
        $user = $request->session()->get("Home");
        if ($request->input('_token')) {
            if (aaa_md5($request->input('Mu_TwoPwd')) != $user->Mu_TwoPwd) {
                return aaa_dispatch(0, "二级密码错误", $request->url());
            }
            $res = \App\Models\MlcmsUserWithdrawlog::addWithLog($request);
            return aaa_dispatch($res['status'], $res['info'], $request->url());
        }
        $wlogs =\App\Models\MlcmsUserWithdrawlog::where("Mud_Number", $user->Mu_Number)->orderBy("Mud_CreateTime", 'desc')->paginate();
        $this->share($request);
        return view("home.jjwithdraw",compact('wlogs'));
    }
    /**
     * 用户奖金币转电子币
     * @param Request $request
     */
    public function userTransfer(Request $request) {
        $user = $request->session()->get("Home");
        if ($request->input("_token")) {
            $res = \App\Models\MlcmsUsertransfer::AddTransferLog($request, 2);
            return aaa_dispatch($res['status'], $res['info'], $request->url());
        }
        $utlogs = \App\Models\MlcmsUsertransfer::where("Mut_Number", $user->Mu_Number)->where('Mut_Type', 2);
        if ($request->input('starttime')) {
            $utlogs->where('Mut_CreateTime', ">", $request->input('starttime'));
        }
        if ($request->input('endtime')) {
            $utlogs->where("Mut_CreateTime", "<", $request->input('endtime'));
        }
        $utlogs = $utlogs->OrderBy('Mut_CreateTime', 'desc')->paginate();
        $this->share($request);
        return view("home.usertransfer",compact('utlogs','request'));
    }
    /**
     * 电子币互转
     * @param Request $request
     * @return type
     */
    public function dzbhz(Request $request) {
         $user = $request->session()->get("Home");
        if ($request->input("_token")) {
            $res = \App\Models\MlcmsUsertransfer::AddTransferLog($request, 1);
            return aaa_dispatch($res['status'], $res['info'], $request->url());
        }
        $utlogs = \App\Models\MlcmsUsertransfer::where("Mut_Number", $user->Mu_Number)->where('Mut_Type', 1);
        if ($request->input('starttime')) {
            $utlogs->where('Mut_CreateTime', ">", $request->input('starttime'));
        }
        if ($request->input('endtime')) {
            $utlogs->where("Mut_CreateTime", "<", $request->input('endtime'));
        }
        $utlogs = $utlogs->OrderBy('Mut_CreateTime', 'desc')->paginate();
        $this->share($request);
        return view("home.dzbhz",compact('utlogs','request'));
    }
    
    /**
     * 电子币充值
     * @param Request $request
     * @return type
     */
    public function dzbcz(Request $request) {
        $user = $request->session()->get("Home");
        if($request->input("_token")) {
            $res = \App\Models\MlcmsAgentmoneylog::AddAgentMoneylog($request);
            return aaa_dispatch($res['status'], $res['info'], $request->url());
        }
        $utlogs = \App\Models\MlcmsAgentmoneylog::where("Mag_Number", $user->Mu_Number)->paginate();
        $this->share($request);
        return view("home.dzbcz",compact('utlogs'));
    }
    
    /**
     * 注册会员
     * @param Request $request
     */
    public function reguser(Request $request) {
        
        $this->share($request);
        return view("home.reguser");
    }
    
}
